﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AntikCompta.Data;

namespace AntikCompta
{
    public partial class VentesWnd : Form
    {
        private Exercice mExercice;
        private Stock mStock;
        private DataMng mDataMng;
        internal VentesWnd(ref DataMng aDataMng, ref Stock aStock, ref Exercice aExercice)
        {
            mStock = aStock;
            mExercice = aExercice;
            mDataMng = aDataMng;
            InitializeComponent();

            mStock.DataChanged += new DataChangedHandler(mStock_DataChanged);
            CouleurLotMng.GetObject().ColorChanged += new DataChangedHandler(VentesWnd_ColorChanged);
            Text += " " + mExercice.Annee;
            RefreshView();
        }

        void VentesWnd_ColorChanged()
        {
            RefreshView();
        }

        void mStock_DataChanged()
        {
            RefreshView();
        }

        private void RefreshView()
        {
            mStockView.Rows.Clear();
            List<Produit> liste;
            mStock.GetVentes(mExercice.Annee, out liste);

            float valeur = 0;
            if (liste.Count > 0)
            {
                mStockView.Rows.Add(liste.Count);
                
                int row = 0;
                while (row < liste.Count)
                {
                    mStockView.Rows[row].Cells[0].Value = liste[row].Reference;
                    mStockView.Rows[row].Cells[1].Value = liste[row].DateVente;
                    mStockView.Rows[row].Cells[2].Value = liste[row].Nom;
                    mStockView.Rows[row].Cells[3].Value = liste[row].PrixVente;
                    mStockView.Rows[row].Cells[4].Value = liste[row].Acheteur.Nom;
                    mStockView.Rows[row].Cells[5].Value = liste[row].ModePayementVente.Nom;
                    mStockView.Rows[row].Cells[6].Value = liste[row].FactureVente;

                    Color couleur;
                    if (CouleurLotMng.GetObject().GetColor(Produit.GetLotRef(liste[row].Reference), out couleur))
                        mStockView.Rows[row].DefaultCellStyle.BackColor = couleur;

                    valeur += liste[row].PrixVente;
                    row++;
                }
            }
            SAI_Tot.Text = "Total des ventes : " + Math.Round(valeur, 2);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Fermer();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            NewVente wnd = new NewVente(ref mStock, ref mExercice);
            if (wnd.ShowDialog() == DialogResult.OK)
            {
                Produit prod;
                if (wnd.GetProduit(out prod))
                {
                    Exercice ex;
                    if (mDataMng.GetExercice(prod.DateAchat.Year, out ex) )
                        ex.SetProduit(ref prod);
                }
            }
        }

        private void Fermer()
        {
            Close();
        }

        private void mStockView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            String reference = mStockView.Rows[e.RowIndex].Cells[0].Value.ToString();
            Produit prod;
            if (mStock.GetProduit(reference, out prod) == false)
                return;

            Exercice ex;
            if (mDataMng.GetExercice(prod.DateAchat.Year, out ex) == false)
                return;

            NewVente wnd = new NewVente(ref mExercice, ref prod);
            if (wnd.ShowDialog() == DialogResult.OK)
                ex.SetProduit(ref prod);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (mStockView.SelectedRows == null)
                return;

            if (mStockView.SelectedRows.Count <= 0)
                return;

            String reference = mStockView.SelectedRows[0].Cells[0].Value.ToString();
            Produit prod;
            if (mStock.GetProduit(reference, out prod) == false)
                return;

            Exercice ex;
            if (mDataMng.GetExercice(prod.DateAchat.Year, out ex) == false)
                return;

            if (MessageBox.Show("Etes vous certain de vouloir supprimer la vente de: " + prod.Nom + ", référence: " + prod.Reference + "\nvendu le: " + prod.DateVente.ToShortDateString() + "?", "Attention", MessageBoxButtons.OKCancel) != DialogResult.OK)
                return;

            if( prod.AnnulerVente() == true)
                ex.SetProduit(ref prod);
        }

        private void VentesWnd_FormClosing(object sender, FormClosingEventArgs e)
        {
            mStock.DataChanged -= new DataChangedHandler(mStock_DataChanged);
            CouleurLotMng.GetObject().ColorChanged -= new DataChangedHandler(VentesWnd_ColorChanged);
        }

        private void mStockView_SelectionChanged(object sender, EventArgs e)
        {
            txt_status.Text = "";
            if ((mStockView.SelectedRows == null) || (mStockView.SelectedRows.Count <= 0))
                return;

            float valeur = 0;
            foreach (DataGridViewRow row in mStockView.SelectedRows)
            {
                if ( row.Cells[3].Value!=null)
                    valeur += (float)row.Cells[3].Value;
            }
            txt_status.Text = "somme = " + Math.Round(valeur, 2) + " €";
        }

        private void bt_couleur_Click(object sender, EventArgs e)
        {
            if ((mStockView.SelectedRows == null) || (mStockView.SelectedRows.Count <= 0))
                return;

            String reference = mStockView.SelectedRows[0].Cells[0].Value.ToString();

            CouleurLotWnd wnd = new CouleurLotWnd(Produit.GetLotRef(reference));
            wnd.ShowDialog();
        }
    }
}
